package com.lanyou.esb.web;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.repository.FieldSystemRepository;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.esb.datadictionary.FtpHostType;
import com.lanyou.esb.entity.Ftp;
import com.lanyou.esb.entity.FtpServer;
import com.lanyou.esb.entity.Http;
import com.lanyou.esb.entity.Routing;
import com.lanyou.esb.entity.Token;
import com.lanyou.esb.repository.FtpRepository;
import com.lanyou.esb.repository.FtpServerRepository;
import com.lanyou.esb.repository.HttpRepository;
import com.lanyou.esb.repository.TokenRepository;
import com.lanyou.esb.vo.RoutingVo;

/**
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/tokens")
public class TokenController {
	@Autowired
	UserSessionService userSessionService;

	@PersistenceContext
	private EntityManager entityManager;

	@Autowired
	private TokenRepository tokenRepository;

	@Autowired
	private FieldSystemRepository fieldSystemRepository;

	@Autowired
	private HttpRepository httpRepository;
	
	@Autowired
	private FtpRepository ftpRepository;
	
	@Autowired
	private FtpServerRepository ftpServerRepository;

	@RequestMapping(value = "/getRoutes", produces = "application/json")
	public RoutingVo routes(String interfaceId) {
		RoutingVo  routing = new RoutingVo();
		if (interfaceId != null) {
			Http http = httpRepository.findOne(interfaceId);
			List<String> appSystems = new ArrayList<String>();
			List<String> pubSystems = new ArrayList<String>();
 			if (http != null) {
 				routing.setInterfaceName(http.getName());
 				routing.setInterfaceType(http.getIfTypeForDisplay());
 				List<Token> tokens = tokenRepository.findByInterfaceId(interfaceId);
				if (tokens.size() != 0) {
					for (Token token : tokens) {
						String pubSystemId = token.getInterfacePubSystemId();
						String appSystemId = token.getInterfaceCallSystemId();
						FieldSystem pubSystem = fieldSystemRepository
								.findById(pubSystemId);
						String pubSystemName = pubSystem.getName();
						FieldSystem appSystem = fieldSystemRepository
								.findById(appSystemId);
						String appSystemName = appSystem.getName();
						
						if(appSystems.size()==0){
							appSystems.add(appSystemName);
						}else{
							if(!appSystems.contains(appSystemName)){
								appSystems.add(appSystemName);
							}
						}
						if(pubSystems.size()==0){
							pubSystems.add(pubSystemName);
						}else{
							if(!pubSystems.contains(pubSystemName)){
								pubSystems.add(pubSystemName);
							}
						}
					}
				}
			}else{
				Ftp ftp = ftpRepository.findOne(interfaceId);
				if(ftp!=null){
					routing.setInterfaceName(ftp.getInterfaceName());
					routing.setInterfaceType("FTP");
					List<FtpServer> ftpServerForPubSystems = ftpServerRepository.findByFtpAndHostType(ftp, FtpHostType.SOURCE_HOST.ValueCode);
					List<FtpServer> ftpServerForAppSystems = ftpServerRepository.findByFtpAndHostType(ftp, FtpHostType.TARGET_HOST.ValueCode);
					for(FtpServer ftpServer:ftpServerForPubSystems){
						pubSystems.add(ftpServer.getOwnFieldSystem().getName()+" ip:"+ftpServer.getHost());
					}
					for(FtpServer ftpServer:ftpServerForAppSystems){
						appSystems.add(ftpServer.getOwnFieldSystem().getName()+" ip:"+ftpServer.getHost());
					}
				}
			}
 			routing.setAppSystems(appSystems);
 			routing.setPubSystems(pubSystems);
		}
		return routing;
	}

	@RequestMapping(value = "/getRouteTables", produces = "application/json")
	public Page<Routing> routeTable(String fieldId, String systemId,
			String interfaceName, String appSystemName, Pageable pageable) {
		User user = userSessionService.getCurrentAuthenticatedUser();
		String userId = user.getId();
		if (fieldId == null) {
			fieldId = "";
		}
		if (systemId == null) {
			systemId = "";
		}
		if (interfaceName == null) {
			interfaceName = "";
		} else {
			interfaceName = "%" + interfaceName + "%";
		}
		if (appSystemName == null) {
			appSystemName = "";
		} else {
			appSystemName = "%" + appSystemName + "%";
		}
		int page1, page2;

		page1 = pageable.getPageNumber()*pageable.getPageSize();
		page2 = pageable.getPageSize();

		List<Object[]> routings = tokenRepository.findForRouting(interfaceName,
				fieldId, systemId, appSystemName, userId, page1, page2);
		List<Object[]> routings1 = tokenRepository.countForRouting(
				interfaceName, fieldId, systemId, appSystemName, userId);
		List<Routing> result = new ArrayList<Routing>(routings.size());
		int total = routings1.size();
		for (Object[] routing : routings) {
			Routing routingss = new Routing();
			routingss.setFieldName(routing[0] != null ? (String) routing[0]
					: "");
			routingss.setFieldId(routing[1] != null ? (String) routing[1] : "");
			routingss.setSystemName(routing[2] != null ? (String) routing[2]
					: "");
			routingss
					.setSystemId(routing[3] != null ? (String) routing[3] : "");
			routingss.setInterfaceName(routing[4] != null ? (String) routing[4]
					: "");
			routingss.setInterfaceId(routing[5] != null ? (String) routing[5]
					: "");
			routingss.setAppSystemName(routing[6] != null ? (String) routing[6]
					: "");
			result.add(routingss);
		}
		Page<Routing> page3 = new PageImpl<Routing>(result, pageable, total);
		return page3;
	}

}
